<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="参数名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入参数名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="参数Code" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" placeholder="请输入参数Code"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="参数类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag type="list" v-model="model.type" dictCode="sys_config_type" placeholder="请选择参数类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.type == 'input'">
            <a-form-model-item label="内容文字" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="value">
              <a-input v-model="model.value" placeholder="请输入内容文字"  ></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="24" v-if="model.type == 'textarea'">
            <a-form-model-item label="内容详情" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="value">
              <a-textarea v-model="model.value" rows="4" placeholder="请输入内容详情" />
            </a-form-model-item>
          </a-col>

          <a-col :span="24" v-if="model.type == 'image-cropper'">
            <a-form-model-item label="裁剪图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="value">
              <j-image-cropper  :height="200" :width="380" v-model="model.value"  ></j-image-cropper>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.type == 'image-water'">
            <a-form-model-item label="水印图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="value">
              <j-image-upload  watermark="true" v-model="model.value"  ></j-image-upload>
            </a-form-model-item>
          </a-col>

          <a-col :span="24" v-if="model.type == 'image'">
            <a-form-model-item label="上传图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="value">
              <j-image-upload  v-model="model.value"  ></j-image-upload>
            </a-form-model-item>
          </a-col>

          <a-col :span="24" v-if="model.type == 'mulitimage'">
            <a-form-model-item label="上传图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="value">
              <j-image-upload is-multiple v-model="model.value"  ></j-image-upload>
            </a-form-model-item>
          </a-col>

          <a-col :span="24" v-if="model.type == 'file'">
            <a-form-model-item label="上传文件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="value">
              <j-upload  v-model="model.value"  ></j-upload>
            </a-form-model-item>
          </a-col>

          <a-col :span="24" v-if="model.type == 'editor'">
            <a-form-model-item label="富文详情" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="value">
              <j-editor v-model="model.value" :height=500  watermark="false" />
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="参数位置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="position">
              <a-input-number v-model="model.position" placeholder="请输入位置" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="隐藏参数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isHide">
              <a-radio-group name="isHide" v-model="model.isHide">
                <a-radio value="0">否</a-radio>
                <a-radio value="1">是</a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'SysConfigForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
          type :'input',
          position:1,
          isHide:'0'
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           name: [
              { required: true, message: '请输入参数名称!'},
           ],
           code: [
              { required: true, message: '请输入参数Code!'},
              { validator: (rule, value, callback) => validateDuplicateValue('sys_config', 'code', value, this.model.id, callback)},
           ],
           type: [
              { required: true, message: '请输入参数类型!'},
           ],
           value: [
              { required: true, message: '请输入参数数据值!'},
           ],
           position: [
              { required: true, message: '请输入位置!'},
           ],
           isHide: [
              { required: true, message: '请输入是否 隐藏 0 否 1 是!'},
           ],
        },
        url: {
          add: "/sys/config/add",
          edit: "/sys/config/edit",
          queryById: "/sys/config/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>